package UIFree.graphicTools.angle
{
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_util;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class uif_directCtrl extends uif_ecomponent {
		private var _limb			: uif_radialLimb;
		private var _directLayer	: uif_directView;
		private var _buttonDown		: Boolean;
		private var _info			: uif_label;
		
		public function uif_directCtrl(parent:uif_component, ax:int=0, ay:int=0, awidth: Number=100, aheight: Number=100, styleClass:Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			createComponents();
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(MouseEvent.MOUSE_DOWN, doDown);
			addEvent(MouseEvent.MOUSE_UP, doUp, stage);
			addEvent(MouseEvent.MOUSE_MOVE, doMove, stage);
		}
		
/*		
		private function doMove(e: MouseEvent): void {
			_directLayer.directPos = _directLayer.mousePos;
		}
		
		private function doOver(e: MouseEvent): void {
			_safePos = _directLayer.directPos;
		}
		
		private function doOut(e: MouseEvent): void {
			_directLayer.directPos = _safePos;
		}*/
		
		protected function doDown(e: MouseEvent): void {
			_buttonDown = true;
			updateDirect();
		}
		
		protected function doUp(e: MouseEvent): void {
			_buttonDown = false;
		}
		
		protected function doMove(e: MouseEvent): void {
			if (_buttonDown) updateDirect();
		}
		
		protected function updateDirect(): void {
			var p: Point = _directLayer.mousePos.subtract(_directLayer.center);
			if (p.length > radius) p.normalize(radius)
			_directLayer.directPos = _directLayer.center.add(p);
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		protected function updateInfo(): void {
			_info.text = 'angle: ' + Math.round(_directLayer.angle) + '\n' + 
						'length: ' + uif_util.round(_directLayer.length, 4);
		}
		
		public function get angle(): Number {
			return _directLayer.angle;
		}
		
		public function set angle(a_angle: Number): void {
			_directLayer.angle = a_angle;
		}
		
		public function get length(): Number {
			return _directLayer.length;
		}
		
		public function set length(a_length: Number): void {
			_directLayer.length = a_length;
		}
		
		public function get radius(): Number {
			return _directLayer.getInner().height / 2;
		}
		
		private function doChange(e: Event): void {
			updateInfo();
		}
		
		protected function createComponents(): void {
			var inner: Rectangle = getInner();
			var rw: Number = inner.width - inner.height;
			_limb = new uif_radialLimb(this, inner.x, inner.y, inner.height, style.limb);
			_directLayer = new uif_directView(this, inner.x, inner.y, inner.height, inner.height, style.direct);
			_directLayer.addEvent(Event.CHANGE, doChange);
			if (rw > 50) {
				_info = new uif_label(this, inner.x + inner.height, inner.y, rw, inner.height, style.info);
			}
		}
	}
}